<?php
/**
 * Library Of Shared Code (LOSC)
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   LOSC Framework
 * @package    View
 * @subpackage Helper
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */

/**
 * View helper for formatting dates
 *
 * @category   LOSC Framework
 * @package    View
 * @subpackage Helper
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */
class Losc_View_Helper_DateFormat extends Zend_View_Helper_Abstract
{
    /**
     * Date object
     *
     * @var Zend_Date
     */
    private $_date;
    
    /**
     * Current year
     *
     * @var int
     */
    private $_year;
    
    /**
     * DateTime formats
     *
     * @var array
     */
    private $_formats;
    
    /**
     * Creates helper
     *
     */
    public function __construct()
    {
        $user = App_User::getCurrentUser();
        
        // use only one date to save some memory (hopefully)
        $this->_date = Zend_Date::now($user->locale);
        //$this->_date->setTimezone($user->timezone);
        
        // store current year for comparing
        $this->_year = $this->_date->get(Zend_Date::YEAR);
        
        $this->_formats = Zend_Locale_Data::getList($user->locale, 'datetime');
    }
    
    /**
     * Formats the given date
     *
     * @param  string  $date      [optional]
     * @param  boolean $absolute  [optional]
     * @return Wishlist_View_Helper_DateFormat|string
     */
    public function dateFormat($date = null, $absolute = false)
    {
        if ($date === null) {
            return $this;
        }
        
        $this->_date->setTimestamp(strtotime($date));
        
        if ($absolute) {
            return $this->_date->get(Zend_Date::DATE_MEDIUM) . ', ' .
                   $this->_date->get(Zend_Date::TIME_SHORT);
        } elseif ($this->_date->isToday()) {
            // date is today, return localized time (15:53)
            if (array_key_exists('Hm', $this->_formats)) {
                $format = $this->_formats['Hm'];
            } elseif (array_key_exists('HHmm', $this->_formats)) {
                $format = $this->_formats['HHmm'];
            } else {
                $format = 'HH:mm';
            }
            return $this->_date->get($format);
        } elseif ($this->_date->get(Zend_Date::YEAR) == $this->_year) {
            // date is this year, return localized date (Oct 15)
            if (array_key_exists('MMMd', $this->_formats)) {
                $format = $this->_formats['MMMd'];
            } elseif (array_key_exists('Md', $this->_formats)) {
                $format = $this->_formats['Md'];
            } else {
                $format = 'MMM d';
            }
            return $this->_date->get($format);
        } else {
            // date is not even this year, return localized month (Nov 2007)
            if (array_key_exists('yMMM', $this->_formats)) {
                $format = $this->_formats['yMMM'];
            } elseif (array_key_exists('yyMMM', $this->_formats)) {
                $format = $this->_formats['yyMMM'];
            } else {
                $format = 'MMM yyyy';
            }
            return $this->_date->get($format);
        }
    }
}
